import sys
import os
import random
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        toolBar=QToolBar()

        toolBar.addWidget(QLabel('CustomColor:'))
        self.custom=QLineEdit()
        #self.custom.setText('#ffff00')
        toolBar.addWidget(self.custom)

        toolBar.addWidget(QLabel("Options:"))        
        self.optonCombox=QComboBox()
        self.optonCombox.addItems(QColorDialog.ColorDialogOption.__members__.keys())        
        toolBar.addWidget(self.optonCombox)

        btn=QPushButton('ShowColorDialog')
        btn.clicked.connect(self.OnShowDialog)
        toolBar.addWidget(btn)

        layout.addWidget(toolBar) 

        self.label=QLabel('Hello 测试颜色选择')     
        layout.addWidget(self.label)      

        self.setLayout(layout)
    def OnShowDialog(self):
        dialog=QColorDialog()#全局的，每次加的自定义颜色都被保留了        
        if len(self.custom.text())>0:
            index=random.randint(0,dialog.customCount())
            dialog.setCustomColor(index,self.custom.text())
            self.parent.OnShowMessage(f'Set CustomColor {self.custom.text()} at {index}')        
        option=QColorDialog.ColorDialogOption[self.optonCombox.currentText()]        
        color= dialog.getColor(self.label.palette().color(QPalette.ColorRole.WindowText),self,'Select Color',option)
        if color.isValid():
            palette=self.label.palette()
            palette.setColor(QPalette.ColorRole.WindowText,color)
            self.label.setPalette(palette)
            self.parent.OnShowMessage(f'Selected Color:{color.name()}')
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet